package com.example.baserecyclerviewadapterdemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.activity.AnimationUseActivity;
import com.example.baserecyclerviewadapterdemo.activity.EmptyViewUseActivity;
import com.example.baserecyclerviewadapterdemo.activity.HeaderAndFooterUseActivity;
import com.example.baserecyclerviewadapterdemo.activity.HomeActivity;
import com.example.baserecyclerviewadapterdemo.adapter.HomeAdapter;
import com.example.baserecyclerviewadapterdemo.entity.HomeEntity;

import java.util.Arrays;
import java.util.List;

public class BaseUseFragment extends Fragment implements OnItemClickListener {

    private HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_home,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        homeAdapter = new HomeAdapter(R.layout.home_item_view,getHomeItemData());
        homeAdapter.setAnimationEnable(true);
        View top = getLayoutInflater().inflate(R.layout.top_view,recyclerView,false);
        homeAdapter.addHeaderView(top);
        homeAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(homeAdapter);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        HomeEntity homeEntity = (HomeEntity) adapter.getData().get(position);
        startActivity(new Intent(getContext(),homeEntity.getActivity()));
    }

    private List<HomeEntity> getHomeItemData(){
        return Arrays.asList(
                new HomeEntity("Animation", AnimationUseActivity.class, R.mipmap.gv_animation),
                new HomeEntity("Header/Footer", HeaderAndFooterUseActivity.class,R.mipmap.gv_header_and_footer),
                new HomeEntity("EmptyView", EmptyViewUseActivity.class,R.mipmap.gv_empty)
        );
    }
}
